<template>
  <div>
     <mianbaoxie name1="权限管理" name2="角色列表"></mianbaoxie>

    <el-row>
      <el-col :span="4">
        <el-button type="primary" @click="dialogVisible = true"
          >添加角色</el-button
        >
        <!-- 添加角色 -->
        <el-dialog
          title="提示"
          :visible.sync="dialogVisible"
          width="30%"
          center
          @close="addUserDialogClear"
        >
          <el-form
            :model="addUserForm"
            :rules="addRoleFromRules"
            ref="addUserFormRef"
            label-width="100px"
          >
            <el-form-item label="角色名称" prop="roleName">
              <el-input v-model="addUserForm.roleName"></el-input>
            </el-form-item>
            <el-form-item label="角色描述" prop="roleDesc">
              <el-input v-model="addUserForm.roleDesc"></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="tianjia">添加用户</el-button>
          </span>
        </el-dialog>
        <!-- 编辑信息 -->
        <el-dialog
        title="修改用户信息"
        :visible.sync="editDialogVisible"
        width="30%"
      >
        <el-form ref="editForm" :model="editRoleData" label-width="80px">
          <!--disabled 禁用  -->
          <el-form-item label="角色id">
            <el-input v-model="editRoleData.userid"></el-input>
          </el-form-item>
          <el-form-item label="角色名称" prop="roleName">
            <el-input v-model="editRoleData.roleName"></el-input>
          </el-form-item>
          <el-form-item label="角色描述" prop="roleDesc">
            <el-input v-model="editRoleData.roleDesc"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editUserInfo"
            >修改用户信息</el-button
          >
        </span>
      </el-dialog>
      </el-col>
    </el-row>
    <!-- 渲染数据 -->
    <el-table class="el-table" :data="listData" style="width: 100%" border>
      <el-table-column type="expand">
        <template slot-scope="scoped">
          <div>
            <el-row
              :class="['row-bottom', index1 == 0 ? 'row-top' : '']"
              v-for="(item1, index1) in scoped.row.children"
              :key="item1.id"
            >
              <el-col :span="5">
                <el-tag closable>{{ item1.authName }}</el-tag>
              </el-col>
              <el-col :span="19">
                <el-row
                  :class="[
                    'row-top',
                    index2 == item1.children.length - 1 ? 'row-bottom' : '',
                  ]"
                  v-for="(item2, index2) in item1.children"
                  :key="item2.id"
                >
                  <el-col :span="6">
                    <el-tag closable type="success">{{
                      item2.authName
                    }}</el-tag>
                  </el-col>
                  <el-col :span="18">
                    <el-tag
                      type="danger"
                      v-for="item3 in item2.children"
                      :key="item3.id"
                      closable
                      >{{ item3.authName }}</el-tag
                    >
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </div>
        </template>
      </el-table-column>
      <el-table-column type="index" label="#"> </el-table-column>
      <el-table-column prop="roleName" label="角色名称"> </el-table-column>
      <el-table-column prop="roleDesc" label="角色描述"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scoped">
          <div>
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="xiugai(scoped.row)"
              >编辑</el-button
            >
            <!--circle 椭圆  -->
            <el-button type="danger" size="mini" icon="el-icon-delete"
            
            @click="shanchu(scoped.row.id)"
              >删除</el-button
            >
            <el-button type="warning" size="mini" icon="el-icon-search"
              >分配权限</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
   
  </div>
</template>

<script>
import mianbaoxie from "@/compoents/mianbaoxie";
import { rolesList, add ,bianji,search_data,delete_user} from "../../../api/roles";
export default {
  data() {
    return {
      listData: [],
      dialogVisible: false,
      addUserForm: {
        roleName: "",
        roleDesc: "",
      },
      editDialogVisible: false,
      // editFormData: {},
      // 表单验证
      addRoleFromRules:{
        roleName:[
          { required: true, message: "请输入角色名", trigger: "blur" },
          {
            min: 3,
            max: 20,
            message: "长度在 3 到 20 个字符",
            trigger: "blur",
          },
        ],
        roleDesc:[
          { required: true, message: "请输入角色描述", trigger: "blur" },
          {
            min: 3,
            max: 20,
            message: "长度在 3 到 20 个字符",
            trigger: "blur",
          },
        ]
      },
      // 修改表单的数据
      editRoleData:{},
    };
  },
  mounted() {
    this.getData();
  },
  components: {
    mianbaoxie,
  },
  methods: {
    // 列表
    async getData() {
      const result = await rolesList();
      console.log(result);
      if (result.meta.status !== 200) {
        return this.$message.error("请求列表失败");
      }
      this.listData = result.data;
    },

    addUserDialogClear() {
      this.$refs.addUserFormRef.resetFields();
    },

    // 添加
    tianjia() {
      this.$refs.addUserFormRef.validate(async (valid) => {
        console.log(valid);
        if (!valid) return;
        const result = await add({
          method: "post",
          url: "roles",
          data: this.addUserForm,
        });
        console.log(result);
        console.log(result.meta);
        if (result.meta.status != 201) {
          return this.$message.error("添加失败");
        }
        this.dialogVisible = false;
        this.getData();
        return this.$message.success("添加成功");
      });
    },
    // 点击users页面上的修改用户按钮触发的事件
    async xiugai(userInfo) {
      console.log(userInfo);
      // 展示用户修改的对话框
      // 根据id查询到用户的信息
      // 请求数据和显示对话框谁在上谁在下 数据请求再上

      const result = await search_data({
        method: "get",
        url: `roles/${userInfo.id}`,
      });
      console.log(result);
      if (result.meta.status !== 200) {
        return this.$message.error("查询数据失败");
      }
      this.editRoleData = result.data;

      this.editDialogVisible = true;
    },
    // 收集到修改的用户信息 前台验证成功之后 发送请求提交服务器
    editUserInfo() {
      this.$refs.editForm.validate(async (valid) => {
        if (!valid) return;
        const result = await bianji({
          method: "put",
          url: `roles/${this.editRoleData.roleId}`,
          data: this.editRoleData,
        });
        console.log(this.editRoleData);
        // put请求的参数写到data当中
        console.log(result);
        if (result.meta.status !== 200) {
          return this.$message.error("修改用户信息失败");
        }
        // 更新table当中的数据
        this.getData();
        this.editDialogVisible = false;
        return this.$message.success("修改用户信息成功");
      });
    },
    // // 删除用户信息
    async shanchu(id) {
      console.log(id);
      //  删除逻辑：
      //           1.确认  您确认要删除xxx  删除 取消
      //           2. 确认删除 获取id 发送ajax删除服务器数据 重新渲染table
      //           3.取消

      // 但凡是promise对象 可以通过async和await将其转变为同步 通过catch处理错误
      const resultConfirm = await this.$confirm(
        "此操作将永久删除该用户, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      console.log(resultConfirm);
      if (resultConfirm == "confirm") {
        const result = await delete_user({
          method: "delete",
          url: `roles/${id}`,
        });
        console.log(result);
        // 删除失败 告诉用户删除失败
        if (result.meta.status != 200) {
          return this.$message.error("用户删除失败");
        }
        // 删除成功 告诉用户删除成功 并且重置数据
        this.$message.success("用户删除成功");
        this.getData();
      }
    },
    
  },
};
</script>